#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# useful scripts
# write zero
# dd if=/dev/zero of=binary.dat bs=1c count=1
# append zero
# dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1

# count zeros
# xxd -b infile | awk 'NF{NF-=1};1' | sed 's/^.*://' | tr -d '\n' | tr -d ' ' | sed 's/1//g' | wc -c
# count last 1024 zeros
# xxd -b running_warning.bin | awk 'NF{NF-=1};1' | sed 's/^.*://' | tr -d '\n' | tr -d ' ' | tail -c 1024 | tr -d '1' | wc -c

# create two files

str="
head -c 256 </dev/urandom >$output_dir/rnd_file.bin
"
echo ""; echo $str; echo ""
eval $str

str="
echo -n $'\xf0' > $output_dir/balanced_file.bin ; \
for i in {1..255} ; do echo -n $'\x0f' >> $output_dir/balanced_file.bin ; done
"
echo ""; echo $str; echo ""
eval $str

# trigger constant warning
# the balanced_file has in the buffer at the end 64 zeroes and 64 ones
# to trigger a warning and not an error we need to add (704-64) ones (or zeros) at the end of the file

str="
cp $output_dir/balanced_file.bin $output_dir/disparity_warning.bin ; \
for i in {1..32} ; do echo -n $'\xff' >> $output_dir/disparity_warning.bin ; done ; \
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-running-err -disparity-war-th 640 -disparity-err-th 704 -no-password \
    --encoding-conf -no-param \
    --logging-conf  -lib-error-log $output_dir/disparity_warning.err -lib-warning-log $output_dir/disparity_warning.war \
    --decode-encode -to-decode $output_dir/disparity_warning.bin -to-encode $output_dir/dummy ; \
    rm $output_dir/dummy
"
echo ""; echo $str; echo ""
eval $str

str="
nlwar=$( cat $output_dir/disparity_warning.war | wc -l ) ; \
nlerr=$( cat $output_dir/disparity_warning.err | wc -l ) ; \
if [ \$nlwar != '1' ] || [ \$nlerr != '0' ] ; then echo FAIL ; exit ; fi ; \
"
echo ""; echo $str; echo ""
eval $str

# trigger constant error
# the balanced_file has in the buffer at the end 64 zeroes and 64 ones
# to trigger a warning and not an error we need to add (704-64) ones (or zeros) at the end of the file

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-running-err -disparity-war-th 639 -disparity-err-th 640 -no-password \
    --encoding-conf -no-param \
    --logging-conf  -lib-error-log $output_dir/disparity_error.err -lib-warning-log $output_dir/disparity_error.war \
    --decode-encode -to-decode $output_dir/disparity_warning.bin -to-encode $output_dir/dummy ; \
    rm $output_dir/dummy
"
echo ""; echo $str; echo ""
eval $str

str="
nlwar=$( cat $output_dir/disparity_error.war | wc -l ) ; \
nlerr=$( cat $output_dir/disparity_error.err | grep -v 'generic error' | wc -l ) ; \
if [ \$nlwar != '0' ] || [ \$nlerr != '1' ] ; then echo FAIL ; exit ; fi ; \
"
echo ""; echo $str; echo ""
eval $str

# do not trigger error or warnings

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-running-err -no-disparity-err -no-password \
    --encoding-conf -no-param \
    --logging-conf -lib-error-log $output_dir/no_disparity_error.err -lib-warning-log $output_dir/no_disparity_error.war \
    --decode-encode -to-decode $output_dir/disparity_warning.bin -to-encode $output_dir/dummy ; \
    rm $output_dir/dummy
"
echo ""; echo $str; echo ""
eval $str

str="
nlwar=$( cat $output_dir/no_disparity_error.war | wc -l ) ; \
nlerr=$( cat $output_dir/no_disparity_error.err | grep -v 'generic error' | wc -l ) ; \
if [ \$nlwar != '0' ] || [ \$nlerr != '0' ] ; then echo FAIL ; exit ; fi ; \
"
echo ""; echo $str; echo ""
eval $str

# trigger running warning
# the rnd_file has in the buffer mixed zeros and ones
# to trigger a warning and not an error we need to add (32 consecutives bits) ones

str="
cp $output_dir/rnd_file.bin $output_dir/running_warning.bin ; \
echo -n $'\x0f' >> $output_dir/running_warning.bin ; \
for i in {1..3} ; do echo -n $'\xff' >> $output_dir/running_warning.bin ; done ; \
echo -n $'\xf0' >> $output_dir/running_warning.bin 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -running-war-th 32 -running-err-th 36 -no-password \
    --encoding-conf -no-param \
    --logging-conf  -lib-error-log $output_dir/running_warning.err -lib-warning-log $output_dir/running_warning.war \
    --decode-encode -to-decode $output_dir/running_warning.bin -to-encode $output_dir/dummy ; \
    rm $output_dir/dummy
"
echo ""; echo $str; echo ""
eval $str

str="
nlwar=$( cat $output_dir/running_warning.war | wc -l ) ; \
nlerr=$( cat $output_dir/running_warning.err | wc -l ) ; \
if [ \$nlwar != '1' ] || [ \$nlerr != '0' ] ; then echo FAIL ; exit ; fi ; \
"
echo ""; echo $str; echo ""
eval $str


# trigger running error
# the rnd_file has in the buffer mixed zeros and ones
# to trigger a warning and not an error we need to add (32 consecutives bits) ones

str="
cp $output_dir/rnd_file.bin $output_dir/running_error.bin ; \
echo -n $'\x0f' >> $output_dir/running_error.bin ; \
for i in {1..3} ; do echo -n $'\xff' >> $output_dir/running_error.bin ; done ; \
echo -n $'\xff' >> $output_dir/running_error.bin 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -running-war-th 32 -running-err-th 36 -no-password \
    --encoding-conf -no-param \
    --logging-conf  -lib-error-log $output_dir/running_error.err -lib-warning-log $output_dir/running_error.war \
    --decode-encode -to-decode $output_dir/running_error.bin -to-encode $output_dir/dummy ; \
    rm $output_dir/dummy
"
echo ""; echo $str; echo ""
eval $str

str="
nlwar=$( cat $output_dir/running_error.war | wc -l ) ; \
nlerr=$( cat $output_dir/running_error.err | grep -v 'generic error' | wc -l ) ; \
if [ \$nlwar != '0' ] || [ \$nlerr != '1' ] ; then echo FAIL ; exit ; fi ; \
"
echo ""; echo $str; echo ""
eval $str


